package com.nado.iims2.blog.action.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.nado.extensions.pagination.PaginationInfo;
import com.nado.iims2.blog.bo.service.NamespaceBO;
import com.nado.iims2.blog.model.service.Namespace;
import com.nado.iims2.blog.util.CommonUtil;

/**
 * Handles requests for the application nspace page.
 */
@Controller
public class NSpaceController {
	/** The logger. */
	private static final Logger logger = LoggerFactory.getLogger(NSpaceController.class);
	
	@Autowired
	private NamespaceBO namespaceBO = null;

	@RequestMapping(value = "/service/nspace", method = RequestMethod.GET)
	public String list(HttpServletRequest request, Model model) {
		int page = NumberUtils.toInt(request.getParameter("page"), 1);
		List<Namespace> namespaceList = new ArrayList<Namespace>();
		PaginationInfo paginationInfo = null;
		
		int count = namespaceBO.getNamespaceListCount();
		paginationInfo = new PaginationInfo("default", request, count);
		namespaceList = namespaceBO.getNamespaceList(CommonUtil.calcOffset(page, paginationInfo.getPageSize()), paginationInfo.getPageSize());
		
		model.addAttribute("nspaceCount", count);
		model.addAttribute("nspaceList", namespaceList);
		model.addAttribute("paginate", paginationInfo);
		return "/service/nspaceList";
	}
	
	@RequestMapping(value = "/service/nspace/form", method = RequestMethod.GET)
	public String form(Model model) {
		Namespace namespace = new Namespace();
		model.addAttribute(namespace);
		return "/service/nspaceForm";
	}
	
	@RequestMapping(value = "/service/nspace/write", method = RequestMethod.POST)
	public String write(@ModelAttribute Namespace namespace) {
		namespace.setUserId("SYSTEM");
		
		namespaceBO.writeNamespace(namespace);
		
		return "redirect:/service/nspace";
	}
} 
